package com.foreknow.demo13;

import java.io.*;

/**
 * 如果一个类实现了Serializable接口，表示Employee可以实现序列化
 */
public class Employee implements Serializable {
    private String name;
    private int age;
    private String dept;

    public Employee(String name,int age,String dept) {
        this.name = name;
        this.age = age;
        this.dept = dept;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public static void main(String[] args) {
        //将Employee对象持久化到本地文件中 ObjectInputStream ObjectOutputStream
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("abc.ser"); //节点流
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);//处理流
            objectOutputStream.writeObject(new Employee("fender",23,"market")); //向文件中写对象
            objectOutputStream.writeObject(new Employee("gibson",33,"program"));
            objectOutputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
